home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / amyboard / xboard-3.3.pl0 / gamelist.c < prev    next >
C/C++ Source or Header  |  1995-08-12  |  8KB  |  302 lines

  1. /*
  2.  * gamelist.c -- Functions to manage a gamelist
  3.  * XBoard $Id: gamelist.c,v 1.5 1995/07/28 05:23:42 mann Exp $
  4.  *
  5.  * Copyright 1995 Free Software Foundation, Inc.
  6.  *
  7.  * ------------------------------------------------------------------------
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  * ------------------------------------------------------------------------
  22.  */
  23.  
  24. #include <config.h>
  25.  
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #if STDC_HEADERS
  29. # include <stdlib.h>
  30. # include <string.h>
  31. #else /* not STDC_HEADERS */
  32. # if HAVE_STRING_H
  33. #  include <string.h>
  34. # else /* not HAVE_STRING_H */
  35. #  include <strings.h>
  36. # endif /* not HAVE_STRING_H */
  37. #endif /* not STDC_HEADERS */
  38.  
  39. #include "common.h"
  40. #include "frontend.h"
  41. #include "backend.h"
  42. #include "parser.h"
  43.  
  44.  
  45. /* Variables
  46.  */
  47. List gameList;
  48.  
  49.  
  50. /* Local function prototypes
  51.  */
  52. static void GameListDeleteGame P((ListGame *));
  53. static ListGame *GameListCreate P((void));
  54. static void GameListFree P((List *));
  55. static int GameListNewGame P((ListGame **));
  56.  
  57. /* Delete a ListGame; implies removint it from a list.
  58.  */
  59. static void GameListDeleteGame(listGame)
  60.     ListGame *listGame;
  61. {
  62.     if (listGame) {
  63.     if (listGame->gameInfo.event) free(listGame->gameInfo.event);
  64.     if (listGame->gameInfo.site) free(listGame->gameInfo.site);
  65.     if (listGame->gameInfo.date) free(listGame->gameInfo.date);
  66.     if (listGame->gameInfo.round) free(listGame->gameInfo.round);
  67.     if (listGame->gameInfo.white) free(listGame->gameInfo.white);
  68.     if (listGame->gameInfo.black) free(listGame->gameInfo.black);
  69.     if (listGame->gameInfo.fen) free(listGame->gameInfo.fen);
  70.     if (listGame->gameInfo.resultDetails) free(listGame->gameInfo.resultDetails);
  71.     if (listGame->gameInfo.timeControl) free(listGame->gameInfo.timeControl);
  72.     if (listGame->gameInfo.extraTags) free(listGame->gameInfo.extraTags);
  73.     ListNodeFree((ListNode *) listGame);
  74.     }
  75. }
  76.  
  77.  
  78. /* Free the previous list of games.
  79.  */
  80. static void GameListFree(gameList)
  81.     List *gameList;
  82. {
  83.     while (!ListEmpty(gameList))
  84.     {
  85.     GameListDeleteGame((ListGame *) gameList->head);
  86.     }
  87. }
  88.  
  89.  
  90.  
  91. /* Initialize a new GameInfo structure.
  92.  */
  93. void GameListInitGameInfo(gameInfo)
  94.     GameInfo *gameInfo;
  95. {
  96.     gameInfo->event = NULL;
  97.     gameInfo->site = NULL;
  98.     gameInfo->date = NULL;
  99.     gameInfo->round = NULL;
  100.     gameInfo->white = NULL;
  101.     gameInfo->black = NULL;
  102.     gameInfo->result = GameUnfinished;
  103.     gameInfo->fen = NULL;
  104.     gameInfo->resultDetails = NULL;
  105.     gameInfo->timeControl = NULL;
  106.     gameInfo->extraTags = NULL;
  107. }
  108.  
  109.  
  110. /* Create empty ListGame; returns ListGame or NULL, if out of memory.
  111.  *
  112.  * Note, that the ListGame is *not* added to any list
  113.  */
  114. static ListGame *GameListCreate()
  115.  
  116. {
  117.     ListGame *listGame;
  118.  
  119.     if ((listGame = (ListGame *) ListNodeCreate(sizeof(*listGame)))) {
  120.     GameListInitGameInfo(&listGame->gameInfo);
  121.     }
  122.     return(listGame);
  123. }
  124.  
  125.  
  126. /* Creates a new game for the gamelist.
  127.  */
  128. static int GameListNewGame(listGamePtr)
  129.      ListGame **listGamePtr;
  130. {
  131.     if (!(*listGamePtr = (ListGame *) GameListCreate())) {
  132.     GameListFree(&gameList);
  133.     return(ENOMEM);
  134.     }
  135.     ListAddTail(&gameList, (ListNode *) *listGamePtr);
  136.     return(0);
  137. }
  138.  
  139.  
  140. /* Build the list of games in the open file f.
  141.  * Returns 0 for success or error number.
  142.  */
  143. int GameListBuild(f)
  144.     FILE *f;
  145. {
  146.     ChessMove cm, lastStart;
  147.     int gameNumber;
  148.     ListGame *currentListGame = NULL;
  149.     int error;
  150.     int offset;
  151.  
  152.     GameListFree(&gameList);
  153.     yynewfile(f);
  154.     gameNumber = 0;
  155.  
  156.     lastStart = (ChessMove) 0;
  157.     yyskipmoves = TRUE;
  158.     do {
  159.     yyboardindex = 1;
  160.     offset = yyoffset();
  161.     cm = (ChessMove) yylex();
  162.     switch (cm) {
  163.       case GNUChessGame:
  164.         if ((error = GameListNewGame(¤tListGame))) {
  165.         rewind(f);
  166.         yyskipmoves = FALSE;
  167.         return(error);
  168.         }
  169.         currentListGame->number = ++gameNumber;
  170.         currentListGame->offset = offset;
  171.         if (currentListGame->gameInfo.event != NULL) {
  172.         free(currentListGame->gameInfo.event);
  173.         }
  174.         currentListGame->gameInfo.event = StrSave(yy_text);
  175.         lastStart = cm;
  176.         break;
  177.       case XBoardGame:
  178.         lastStart = cm;
  179.         break;
  180.       case MoveNumberOne:
  181.         switch (lastStart) {
  182.           case GNUChessGame:
  183.         break;        /*  ignore  */
  184.           case PGNTag:
  185.         lastStart = cm;
  186.         break;        /*  Already started */
  187.           case (ChessMove) 0:
  188.           case MoveNumberOne:
  189.           case XBoardGame:
  190.         if ((error = GameListNewGame(¤tListGame))) {
  191.             rewind(f);
  192.             yyskipmoves = FALSE;
  193.             return(error);
  194.         }
  195.         currentListGame->number = ++gameNumber;
  196.         currentListGame->offset = offset;
  197.         lastStart = cm;
  198.         break;
  199.           default:
  200.         break;        /*  impossible  */
  201.         }
  202.         break;
  203.       case PGNTag:
  204.         lastStart = cm;
  205.         if ((error = GameListNewGame(¤tListGame))) {
  206.         rewind(f);
  207.         yyskipmoves = FALSE;
  208.         return(error);
  209.         }
  210.         currentListGame->number = ++gameNumber;
  211.         currentListGame->offset = offset;
  212.         ParsePGNTag(yy_text, ¤tListGame->gameInfo);
  213.         do {
  214.         yyboardindex = 1;
  215.         offset = yyoffset();
  216.         cm = (ChessMove) yylex();
  217.         if (cm == PGNTag) {
  218.             ParsePGNTag(yy_text, ¤tListGame->gameInfo);
  219.         }
  220.         } while (cm == PGNTag || cm == Comment);
  221.         break;
  222.       default:
  223.         break;
  224.     }
  225.     }
  226.     while (cm != (ChessMove) 0);
  227.  
  228.  
  229.     if (appData.debugMode) {
  230.     for (currentListGame = (ListGame *) gameList.head;
  231.          currentListGame->node.succ;
  232.          currentListGame = (ListGame *) currentListGame->node.succ) {
  233.  
  234.         fprintf(debugFP, "Parsed game number %d, offset %ld:\n",
  235.             currentListGame->number, currentListGame->offset);
  236.         PrintPGNTags(debugFP, ¤tListGame->gameInfo);
  237.     }
  238.     }
  239.  
  240.     rewind(f);
  241.     yyskipmoves = FALSE;
  242.     return 0;
  243. }
  244.  
  245.  
  246. /* Clear an existing GameInfo structure.
  247.  */
  248. void ClearGameInfo(gameInfo)
  249.     GameInfo *gameInfo;
  250. {
  251.     if (gameInfo->event != NULL) {
  252.     free(gameInfo->event);
  253.     }
  254.     if (gameInfo->site != NULL) {
  255.     free(gameInfo->site);
  256.     }
  257.     if (gameInfo->date != NULL) {
  258.     free(gameInfo->date);
  259.     }
  260.     if (gameInfo->round != NULL) {
  261.     free(gameInfo->round);
  262.     }
  263.     if (gameInfo->white != NULL) {
  264.     free(gameInfo->white);
  265.     }
  266.     if (gameInfo->black != NULL) {
  267.     free(gameInfo->black);
  268.     }
  269.     if (gameInfo->resultDetails != NULL) {
  270.     free(gameInfo->resultDetails);
  271.     }
  272.     if (gameInfo->fen != NULL) {
  273.     free(gameInfo->fen);
  274.     }
  275.     if (gameInfo->timeControl != NULL) {
  276.     free(gameInfo->timeControl);
  277.     }
  278.     if (gameInfo->extraTags != NULL) {
  279.     free(gameInfo->extraTags);
  280.     }
  281.  
  282.     GameListInitGameInfo(gameInfo);
  283. }
  284.  
  285. char *
  286. GameListLine(number, gameInfo)
  287.      int number;
  288.      GameInfo *gameInfo;
  289. {
  290.     char *event = (gameInfo->event && strcmp(gameInfo->event, "?") != 0) ?
  291.              gameInfo->event : gameInfo->site ? gameInfo->site : "?";
  292.     char *white = gameInfo->white ? gameInfo->white : "?";
  293.     char *black = gameInfo->black ? gameInfo->black : "?";
  294.     char *date = gameInfo->date ? gameInfo->date : "?";
  295.     int len = 10 + strlen(event) + 3 + strlen(white) + 1 + 
  296.       strlen(black) + 3 + strlen(date) + 1;
  297.     char *ret = (char *) malloc(len);
  298.     sprintf(ret, "%d  %s / %s-%s / %s", number, event, white, black, date);
  299.     return ret;
  300. }
  301.  
  302.